<?php
namespace app\index\controller;

use think\View;
use think\Db;
use think\Session;


class Index
{   
	//首页数据
    public function index()
    {   
    	$view         = new View();
        //获取抽奖信息
        $prize_list = Db::name('prize')->field('id,name')->select();
        //获取用户信息
        $user_info['share_link'] = "https://happy6.top";
        if(Session::has('user_mobile')){
        	$user_info = Db::name('user')->where('mobile',Session::get('user_mobile'))->field('mobile,now_number,share_num')->find();
        	$user_info['share_link'] = "https://happy6.top?share_num={$user_info['share_num']}";
        }
       
        $sign_package = getSignPackage();
        $view->assign('user_info',$user_info);
        $view->assign('sign_package',$sign_package);
        $view->assign('list',$prize_list);
     
        return $view->fetch();
    }
    //分享
    public function doShare(){
        $mobile = Session::get('user_mobile');
        $data['code'] = 0;
        if($mobile){
            $begintime = mktime(0,0,0,date('m'),date('d'),date('Y'));
            //判断是否今天第一次分享
            if(!Db::name('share_record')->where('mobile',$mobile)->where('create_time >'.$begintime)->find()){
               //增加一次抽奖机会
               Db::name('user')->where('mobile',$mobile)->setInc('now_number'); // 用户的积分加1
               $data['code'] = 1;
            }
            //添加分析记录表
            $share_info['mobile']      = $mobile;
            $share_info['create_time'] = time();
            $share_info['type']        = input('type');
            Db::name('share_record')->insert($share_info);
            return json($data);
        }
    }
    
    //用户抽奖记录
    public function prize_list(){

        $where['pr.mobile'] = Session::get('user_mobile');
        if(input('type')){
            $where['p.type'] = input('type'); 
        }

    	$view              = new View();
    	$user_prize_record = Db::name('prize_record')
                            ->alias('pr')
                            ->join('prize p', 'pr.pid = p.id','left')
                            ->join('user u' , 'pr.mobile = u.mobile','left')
                            ->field('p.name,u.mobile,pr.create_time,pr.id')
                            ->order('pr.create_time desc')
                            ->where($where)
                            ->where('p.type>0')
                            ->select();
        $view->assign('list',$user_prize_record);
     
        return $view->fetch();
    }

    //抽奖奖品结果
    public function drawPriceResult(){
    	//判断用户是否是第一次抽奖
    	if(!$result = Db::name('prize_record')->where('mobile',Session::get('user_mobile'))->find()){
    		//如果是第一次抽奖
    		$prize_list = Db::name('prize')->where('is_frist',1)->field('id,name,probability,count')->select();
    	}else{
    		$prize_list = Db::name('prize')->field('id,name,probability,count')->select();
    	}

        foreach ($prize_list as $key => $val) { 
            $arr[$val['id']]            = $val['probability']; 
            $new_prize_list[$val['id']] = $val; 
		} 
       
		$rid   = $this->get_rand($arr); //获奖商品的id 

		$prize = $new_prize_list[$rid]; //中奖项 
	
		//添加用户抽奖记录
		$user_mobile = Session::get('user_mobile');
		$now_time    = time();

		Db::name('prize_record')->insert(['mobile'      => $user_mobile, 'pid'        => $rid,'create_time'=> $now_time]);
        

        if($prize['count'] > 0){
        	$count = $prize['count'] - 1;
			//奖品中奖数量减一
			Db::name('prize')->where('id', $rid)->update(['count' => $count ,]);
			//用户抽奖次数递减-；
			Db::name('user')->where('mobile',Session::get('user_mobile'))->setDec('now_number');
			return json($prize);
        }else{
        	return json(['code' => 0]);
        }
	}

    //抽奖概率值获取中奖商品的id
	public function get_rand($proArr) { 
	    $result = '';  
	    
	    $proSum = array_sum($proArr); //概率数组的总概率精度 
	    //概率数组循环 
	    foreach ($proArr as $key => $proCur) { 
	        $randNum = mt_rand(1, $proSum); 
	        
	        if ($randNum <= $proCur) { 
	            $result = $key; break; 
	        } else { 
	            $proSum -= $proCur; 
	        } 		
	    } 
	    unset ($proArr);  

	    return $result; 
	} 

}
